using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_Province : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadData();
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulUtility = (HtmlGenericControl)Master.FindControl("ulUtility");
        ulUtility.Attributes.Add("class", "current");
        HtmlControl divUtility = (HtmlControl)Master.FindControl("divUtility");
        divUtility.Attributes.Add("class", "select_sub show");
        HtmlGenericControl liprovince = (HtmlGenericControl)Master.FindControl("liprovince");
        liprovince.Attributes.Add("class", "sub_show");

    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        ProvinceCollection _ProvinceCollection = ProvinceService.GetProvinceListByParam(ProvinceColumns.ProvinceId, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        rptList.DataSource = _ProvinceCollection;
        rptList.DataBind();
        ltrNum.Text = _ProvinceCollection.Count.ToString();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;
    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //User _user = (User)e.Item.DataItem;
            Province _Province = (Province)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            //hidID.Value = _user.UserId.ToString();
            hidID.Value = _Province.ProvinceId.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "alternate-row";

            }
        }
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox ckbDelete = (CheckBox)item.FindControl("ckbDelete");
            if (ckbDelete.Checked)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                Criteria criuserPermission = new Criteria();
                criuserPermission.add("[" + ADESSA.Entities.Province._Table + "].[" + ProvinceColumns.ProvinceId + "]", long.Parse(strhidID), Condition.EQUAL, Condition.AND);
                ProvinceCollection _PermissionCollection =ProvinceService.GetProvinceListByParam("[" + ADESSA.Entities.Province._Table + "].[" + ProvinceColumns.ProvinceId+ "]", OrderDirection.ASC, criuserPermission.Criter);
                ADESSA.Entities.Province _Province = new Province();
                if (_PermissionCollection.Count > 0)
                {
                    _Province = _PermissionCollection[0];
                }
                if (Session["Title"] != null)
                {
                    if (Session["Title"].ToString().ToUpper() == FetConstant.Root.ToUpper())
                    {
                        ProvinceService.DeleteProvince(ID);
                        Response.Redirect(Request.Url.ToString());
                    }
                }
                long pro = long.MinValue;
                if (Session["checked"] != null)
                {
                    pro = long.Parse(Session["checked"].ToString());
                    if (_Province.ProvinceId == pro)
                    {
                        return;
                    }
                }
                try
                {
                    ProvinceService.DeleteProvince(ID);
                    Response.Redirect(Request.Url.ToString());
                }
                catch
                {
                    
                }
            }
        }
        LoadData();
    }
    #endregion
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
}
